<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader @create="createFn" :arr="list"></TodoHeader>
    <TodoMain :arr="showArr" @del="deleteFn"></TodoMain>
    <TodoFooter :farr="showArr" @changeType="typeFn" @clear="clearFun"></TodoFooter>
  </section>
</template>

<script>
// 1.0 样式引入
import "./styles/base.css"
import "./styles/index.css"
//1.1引入组件
import TodoHeader from "./components/TodoHeader"
import TodoMain from "./components/TodoMain"
import TodoFooter from "./components/TodoFooter"

export default {
  watch: {
    list: {
      deep: true,
      handler() {
        localStorage.setItem("todoList", JSON.stringify(this.list))
      },
    },
  },
  methods: {
    //7.0
    clearFun() {
      this.list = this.list.filter((obj) => obj.isDone === false)
    },
    createFn(task) {
      //3.4 push到list里面
      let id = this.list.length == 0 ? 100 : this.list.at(-1).id + 1
      this.list.push({
        id: id,
        name: task,
        isDone: false,
      })
    },
    deleteFn(id) {
      //4.0删除事件
      let index = this.list.findIndex((obj) => obj.id === id)
      this.list.splice(index, 1)
    },
    //6.3
    typeFn(str) {
      this.getSel = str
    },
  },

  //1.2注册组件
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      list: JSON.parse(localStorage.getItem("todoList")) || [],
      //6.1
      getSel: "all",
    }
  },
  //6.2
  computed: {
    showArr() {
      if (this.getSel === "yes") {
        return this.list.filter((obj) => obj.isDone === true)
      } else if (this.getSel === "no") {
        return this.list.filter((obj) => obj.isDone === false)
      } else {
        return this.list
      }
    },
  },
}
</script>
